package com.jlboot.model.po.sys;

import com.jlboot.core.Results;
import com.jlboot.core.base.Models;
import com.jfinal.plugin.activerecord.Model;
import com.jlboot.core.consts.Consts;
import com.jlboot.core.plugins.ip.IpKit;
import com.jlboot.core.util.ClientUtil;
import com.jlboot.core.util.LoginUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by 51594 on 2022/7/28.
 */
public class SysWorkLog extends Model<SysWorkLog> {
    public static final SysWorkLog dao = new SysWorkLog().dao();
    public static final String table="sys_work_log";
    public Models mod = Models.n(this);

    public static String getNextId(){
        return SysIdGenerator.dao.getNextId(table);
    }

    public SysWorkLog setCommon(){
        Date now=new Date();
        this.set("id",SysIdGenerator.dao.getNextId(table));
        this.set("del_tag", Consts.COMMON_NONE_DEL);
        this.set("create_time",now);
        this.set("update_time",now);
        //this.set("create_by", LoginUtil.getUserId());
        //this.set("update_by",LoginUtil.getUserId());
        return this;
    }

    /**
     * 日志池
     */
    private static List POOL;

    private static int ADD = 1;
    private static int GET = 2;


    /**
     * 同步池
     *
     * @param types
     * @param worklog
     * @return
     */
    public synchronized static List syncWorklogPool(int types, SysWorkLog worklog) {
        if (POOL == null) POOL = new ArrayList();
        if (types == ADD) {
            POOL.add(worklog);
            return null;
        } else {
            List list = POOL;
            POOL = new ArrayList();
            return list;
        }
    }

    /**
     * 写入日志
     *
     * @param userId
     * @param title
     * @param desc
     * @return
     */
    public Results saveLog(String userId, String title, String desc) {
        SysWorkLog worklog = new SysWorkLog()
                .setCommon()
                .set("log_time", new Date()).set("user_id", userId).set("log_title", title).set("log_desc", desc);
        syncWorklogPool(1, worklog);
        return Results.n();
    }

//    /**
//     * 写入日志
//     *
//     * @param userId
//     * @param title
//     * @param desc
//     * @return
//     */
//    public Results saveLog(String userId, String key1, String title, String desc) {
//        SysWorkLog worklog = new SysWorkLog().set("log_time", new Date()).set("key1", key1).set("user_id", userId)
//                .set("log_title", title).set("log_desc", desc);
//        syncWorklogPool(1, worklog);
//        return Results.n();
//    }

//    /**
//     * 写入日志
//     *
//     * @param userId
//     * @param title
//     * @param desc
//     * @return
//     */
//    public Results saveLog(String userId, String key1, String key2, String title, String desc) {
//        SysWorkLog worklog = new SysWorkLog().set("log_time", new Date()).set("key1", key1).set("key2", key2)
//                .set("user_id", userId).set("log_title", title).set("log_desc", desc);
//        syncWorklogPool(1, worklog);
//        return Results.n();
//    }

//    /**
//     * 写入日志
//     *
//     * @param userId
//     * @param title
//     * @param desc
//     * @return
//     */
//    public Results saveLog(String userId, String key1, String key2, String title, String desc, String ip) {
//        SysWorkLog worklog = new SysWorkLog().set("log_time", new Date()).set("key1", key1).set("key2", key2)
//                .set("user_id", userId).set("log_title", title).set("log_ip", ip).set("log_desc", desc);
//        syncWorklogPool(1, worklog);
//        return Results.n();
//    }


    /**
     * 写入日志
     *
     * @param worklog
     * @return
     */
    public Results saveLog(SysWorkLog worklog) {
        syncWorklogPool(1, worklog);
        return Results.n();
    }

    public void saveLoginLog(HttpServletRequest request, int code, String msg, String loginid){
        String ip = ClientUtil.getIp(request);
        String os = ClientUtil.getClientOS(request);
        String browser = ClientUtil.getClientBrowser(request);
        SysWorkLog worklog = new SysWorkLog();
        worklog.setCommon();
        worklog.set("log_result", code == 0 ? 1 : 2).set("log_title", msg).set("log_time", new Date());
        worklog.set("log_ip", ip).set("log_os", os).set("log_browser", browser).set("log_desc", "");
        worklog.set("log_iparea", IpKit.n().getAddress(ip));
        worklog.set("log_type", 99).set("id", SysWorkLog.getNextId()).set("user_name", "").set("login_id", loginid);

        SysWorkLog.dao.saveLog(worklog);
    }


    /**
     * 同步日志至数据库中
     *
     * @return
     */
    public int scyWorkLog() {
        int succ = 0;
        List<SysWorkLog> list = syncWorklogPool(2, null);
        if (list != null) {
            for (SysWorkLog workLog : list) {
                workLog.setCommon();
                if (workLog.save()) succ++;
            }
        }
        return succ;
    }


}
